package com.zhentao.listener;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.model.common.dtos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleDownOrUpListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @KafkaListener(topics = "articleDownOrUpTopic")
    public void ArticleDownOrUp(String message){
        System.out.println("获取到kafka的数据"+message);
        if(StringUtils.isNotBlank(message)){
            Map map = JSON.parseObject(message,Map.class);
            //修改文章的状态
            QueryWrapper<ApArticleConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleConfig::getArticleId,map.get("articleId"));
            ApArticleConfig config = apArticleConfigMapper.selectOne(queryWrapper);

            int getEnable = (int) map.get("enable");
            boolean enabl = true;
            if(getEnable == 1){
                enabl = false;
            }
            config.setIsDown(enabl);
            apArticleConfigMapper.updateById(config);
        }
    }
}
